package com.neusoft.yiyang.task.controller;

import com.neusoft.yiyang.common.entity.medical.MedicationOrderRecord;
import com.neusoft.yiyang.common.entity.task.CareTask;
import com.neusoft.yiyang.common.enums.TaskStatus;
import com.neusoft.yiyang.task.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "护理任务管理接口", description = "负责护理任务的创建、查询、状态更新及关联业务处理（如药品计划、反馈提交等）")
@RestController
@RequestMapping("/tasks")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Operation(summary = "创建护理任务", description = "新增护理任务（含任务类型、执行时间、关联客户等信息）")
    @PostMapping("/create")
    public void createTask(
            @Parameter(description = "护理任务信息（含任务类型、客户ID、执行人等）", required = true)
            @RequestBody CareTask careTask) {
        taskService.createTask(careTask);
    }

    @Operation(summary = "根据ID查询护理任务", description = "获取指定护理任务的详细信息")
    @GetMapping("/{careTaskId}")
    public CareTask getTaskById(
            @Parameter(description = "护理任务ID", required = true)
            @PathVariable Integer careTaskId) {
        return taskService.getTaskById(careTaskId);
    }

    @Operation(summary = "获取所有护理任务", description = "查询系统中所有的护理任务记录")
    @GetMapping("/all")
    public List<CareTask> getAllTasks() {
        return taskService.getAllTasks();
    }

    @Operation(summary = "查询客户的护理任务", description = "获取指定客户的所有护理任务")
    @GetMapping("/customer/{customerId}")
    public List<CareTask> getTasksByCustomerId(
            @Parameter(description = "客户ID", required = true)
            @PathVariable Integer customerId) {
        return taskService.getTasksByCustomerId(customerId);
    }

    @Operation(summary = "查询护工的护理任务", description = "获取指定护工负责的所有护理任务")
    @GetMapping("/caretaker/{caretakerId}")
    public List<CareTask> getTasksByCaretakerId(
            @Parameter(description = "护工ID", required = true)
            @PathVariable Integer caretakerId) {
        return taskService.getTasksByCaretakerId(caretakerId);
    }

    @Operation(summary = "更新护理任务", description = "修改护理任务的基本信息（如执行时间、任务内容等）")
    @PutMapping("/update/{careTaskId}")
    public void updateTask(
            @Parameter(description = "护理任务ID", required = true) @PathVariable Integer careTaskId,
            @Parameter(description = "更新后的护理任务信息", required = true) @RequestBody CareTask careTask) {
        careTask.setCareTaskId(careTaskId);
        taskService.updateTask(careTask);
    }

    @Operation(summary = "删除护理任务", description = "移除指定的护理任务记录")
    @DeleteMapping("/delete/{careTaskId}")
    public void deleteTask(
            @Parameter(description = "护理任务ID", required = true) @PathVariable Integer careTaskId) {
        taskService.deleteTask(careTaskId);
    }

    @Operation(summary = "处理客户信息变更", description = "当客户信息更新时，同步调整关联的护理任务（如护理级别、护工分配等）")
    @PostMapping("/customer-updated")
    public void handleCustomerUpdate(
            @Parameter(description = "变更信息的客户ID", required = true)
            @RequestParam("customerId") Integer customerId) {
        taskService.handleCustomerUpdateForNursingTasks(customerId);
    }

    @Operation(summary = "更新护理任务状态", description = "修改护理任务的执行状态（如待执行、已完成等），支持上传反馈文件和填写执行详情")
    @PostMapping("/nursing/{taskId}/update-status")
    public void updateNursingTaskStatus(
            @Parameter(description = "护理任务ID", required = true) @PathVariable("taskId") Integer taskId,
            @Parameter(description = "任务状态（如PENDING/COMPLETED/CANCELLED）", required = true) @RequestParam("status") TaskStatus status,
            @Parameter(description = "执行人ID（可选，更新任务的实际执行人）") @RequestParam(value = "caretakerId", required = false) Integer caretakerId,
            @Parameter(description = "执行数量（可选，如药品服用剂量）") @RequestParam(value = "quantity", required = false) Integer quantity,
            @Parameter(description = "执行反馈（可选，记录执行情况或异常）") @RequestParam(value = "feedback", required = false) String feedback,
            @Parameter(description = "反馈文件（可选，如执行凭证照片）") @RequestParam(value = "file", required = false) MultipartFile file) {
        taskService.updateNursingTaskStatus(taskId, status, caretakerId, quantity, feedback, file);
    }

    @Operation(summary = "查询客户的药品计划任务", description = "获取指定客户的药物相关护理任务及关联药品信息")
    @GetMapping("/getMedicationInfoByCustomerId/{customerId}")
    public List<Map<String, Object>> getMedicationInfoByCustomerId(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId) {
        return taskService.getMedicationInfoByCustomerId(customerId);
    }

    @Operation(summary = "提交药物任务反馈", description = "完成药物护理任务后，提交执行记录（同步更新任务状态和药物执行记录）")
    @PostMapping("/submitMedicationFeedback")
    public void submitMedicationFeedback(
            @Parameter(description = "护理任务ID", required = true) @RequestParam("careTaskId") Integer careTaskId,
            @Parameter(description = "药物执行记录信息（含执行时间、剂量等）", required = true) @RequestBody MedicationOrderRecord medicationOrderRecord) {
        taskService.submitMedicationFeedback(careTaskId, medicationOrderRecord);
    }

    @Operation(summary = "删除药物相关护理任务", description = "根据客户ID和药物医嘱ID，删除关联的护理任务")
    @PostMapping("/deleteByCidAndMOId/{customerId}/{medicationOrderId}")
    public void deleteMedicationTask(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId,
            @Parameter(description = "药物医嘱ID", required = true) @PathVariable Integer medicationOrderId) {
        taskService.deleteMedicationTask(customerId, medicationOrderId);
    }
}